﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Reporting.WinForms;
using System.Drawing.Printing;
using System.Drawing.Imaging;

namespace DGTestApp
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form3_Load);
            this.FormClosed += new FormClosedEventHandler(Form3_FormClosed);
        }

        void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (m_streams != null)
            {
                foreach (Stream stream in m_streams)
                    stream.Close();
            }
        }

        void Form3_Load(object sender, EventArgs e)
        {
            Run();
        }

        private int m_currentPageIndex;
        private IList<Stream> m_streams;

        private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
        {
            Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);
            m_streams.Add(stream);
            return stream;
        }

        private void Export(Report report)
        {
            string deviceInfo = "<DeviceInfo>"
                + "  <OutputFormat>EMF</OutputFormat>"
                + "  <PageWidth>8.5in</PageWidth>"
                + "  <PageHeight>11in</PageHeight>"
                + "  <MarginTop>0.25in</MarginTop>"
                + "  <MarginLeft>0.25in</MarginLeft>"
                + "  <MarginRight>0.25in</MarginRight>"
                + "  <MarginBottom>0.25in</MarginBottom>"
                + "</DeviceInfo>";

            Warning[] warnings;
            //Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string extension;

            m_streams = new List<Stream>();

            byte[] buffer = report.Render("Image", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);

            foreach (string s in streamids)
            {
                m_streams.Add(new MemoryStream());
            }
        }

        private void PrintPage(object sender, PrintPageEventArgs ev)
        {
            Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
            ev.Graphics.DrawImage(pageImage, 0, 0);
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }
        private void Print()
        {
             string printerName = new PrinterSettings().PrinterName;
            if (m_streams == null || m_streams.Count == 0) return;
            PrintDocument printDoc = new PrintDocument();
            printDoc.PrinterSettings.PrinterName = printerName;
            if (!printDoc.PrinterSettings.IsValid)
            {
                string msg = String.Format("Can't find printer \"{0}\".", printerName);
                MessageBox.Show(msg);
            }
            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            printDoc.Print();
        }

        private void Run()
        {
            ReportViewer vw = new ReportViewer();
            ServerReport report = vw.ServerReport;
            report.ReportPath = "/BillingReports/InvoiceDetails";
            report.ReportServerCredentials.NetworkCredentials = new System.Net.NetworkCredential("administrator", "stb2007");
            report.ReportServerUrl = new Uri("http://billingdb/reportserver");
            ReportParameter p = new ReportParameter();
            p.Name = "invoiceID";
            p.Values.Add("305");

            report.SetParameters(new ReportParameter[1] { p });
            report.Refresh();

            Export(report);
            m_currentPageIndex = 0;
            Print();
        }

    }
}
